package map;

import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

public class PointGenerator {

    private static final int MIN_X = -1700;
    private static final int MAX_X = 1700;
    private static final int MIN_Y = -1700;
    private static final int MAX_Y = 1700;
    private static final double INTERVAL = 20.0; // 间隔至少为20

    private static final int CITY_COUNT = 600;
    private static final int MONSTER_COUNT = 1500;
    private static final int GATHER_COUNT = 800;
    private static final int KACOSCOMP_COUNT = 600;

    public static void main(String[] args) {
        Map<String, List<Point>> points = generatePoints();
        savePointsToJson("points1.json", points);
    }

    private static Map<String, List<Point>> generatePoints() {
        Map<String, List<Point>> allPoints = new HashMap<>();
        allPoints.put("city", new ArrayList<>());
        allPoints.put("monster", new ArrayList<>());
        allPoints.put("gather", new ArrayList<>());
        allPoints.put("kacoscomp", new ArrayList<>());

        Random random = new Random();

        // 为每种类型生成点
        generatePointsOfType("city", CITY_COUNT, allPoints, random);
        generatePointsOfType("monster", MONSTER_COUNT, allPoints, random);
        generatePointsOfType("gather", GATHER_COUNT, allPoints, random);
        generatePointsOfType("kacoscomp", KACOSCOMP_COUNT, allPoints, random);

        return allPoints;
    }

    private static void generatePointsOfType(String type, int count, Map<String, List<Point>> allPoints, Random random) {
        List<Point> points = allPoints.get(type);

        while (points.size() < count) {
            double x = generateCoordinate(MIN_X, MAX_X, INTERVAL, random);
            double y = generateCoordinate(MIN_Y, MAX_Y, INTERVAL, random);
            Point point = new Point(x, y);

            // 检查新点是否与已有点过于接近
            if (isValidPoint(point, allPoints)) {
                points.add(point);
            }
        }
    }

    private static double generateCoordinate(double min, double max, double interval, Random random) {
        // 为了确保间隔，我们先确定一个大致的网格位置，然后添加随机偏移量（但不超过间隔的一半）
        int gridPosition = (int) ((random.nextDouble() * ((max - min) / interval + 1)) * interval);
        double offset = random.nextDouble() * interval / 2; // 随机偏移量，但不超过间隔的一半
        return min + gridPosition + offset;
    }

    private static boolean isValidPoint(Point point, Map<String, List<Point>> allPoints) {
        for (List<Point> points : allPoints.values()) {
            for (Point existingPoint : points) {
                if (Math.sqrt(Math.pow(point.x - existingPoint.x, 2) + Math.pow(point.y - existingPoint.y, 2)) < INTERVAL) {
                    return false;
                }
            }
        }
        return true;
    }

    private static void savePointsToJson(String fileName, Map<String, List<Point>> points) {
        try (FileWriter file = new FileWriter(fileName)) {
            file.write("{\n");
            boolean firstKey = true;
            for (Map.Entry<String, List<Point>> entry : points.entrySet()) {
                if (!firstKey) {
                    file.write(",\n");
                }
                firstKey = false;
                file.write("    \"" + entry.getKey() + "\": [\n");

                boolean firstPoint = true;
                for (Point p : entry.getValue()) {
                    if (!firstPoint) {
                        file.write(",\n");
                    }
                    firstPoint = false;
                    file.write("        {\"x\": " + p.x + ", \"y\": " + p.y + "}");
                }
                file.write("\n    ]");
            }
            file.write("\n}");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 假设你有一个Point类，它有两个字段x和y
    static class Point {
        double x;
        double y;

        Point(double x, double y) {
            this.x = x;
            this.y = y;
        }
    }
}